<template>
    <div>
        <el-button :icon="Plus" type="primary" :disabled="!categoryStore.category3Id" @click="toAddSpuInfo">
            添加SPU</el-button>
        <el-table border :data="spuList" style="margin:20px 0">
            <el-table-column label="序号" type="index" align="center" width="80px"></el-table-column>
            <el-table-column label="SPU名称" prop="spuName"></el-table-column>
            <el-table-column label="SPU描述" prop="description"></el-table-column>
            <el-table-column label="操作">
                <template v-slot="{ row, $index }">
                    <el-button size="small" type="primary" title="添加SKU" :icon="Plus" @click="toAddSkuInfo(row)">
                    </el-button>
                    <el-button size="small" type="primary" title="修改SPU" :icon="Edit" @click="toEditSpuInfo(row)">
                    </el-button>
                    <el-button size="small" type="info" title="查看SKU列表" :icon="InfoFilled"
                        @click="toCheckSkuList(row.id)">
                    </el-button>
                    <el-popconfirm confirm-button-text=" 确认" cancel-button-text="取消" :icon="InfoFilled"
                        icon-color="orange" title="确定要删除此属性值吗?" @confirm="deleteSpuInfo(row.id)">
                        <template #reference>
                            <el-button title="删除" size="small" type="danger" :icon="Delete" />
                        </template>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination v-model:currentPage="currentPage" v-model:page-size="pageSize" :page-sizes="[3, 6, 9, 12]"
            :small="small" :disabled="disabled" :background="background"
            layout=" prev, pager, next, jumper,->, sizes,total" :total="total" @size-change="handleSizeChange"
            @current-change="handleCurrentChange" />


        <!-- 表格 -->
        <el-dialog v-model="dialogSkuVisible" title="显示Sku列表">
            <el-table :data="skuList">
                <el-table-column property="skuName" label="名称" width="150" />
                <el-table-column property="price" label="价格" width="200" />
                <el-table-column property="weight" label="重量(千克)" />
                <el-table-column label="默认图片">
                    <template v-slot="{ row }">
                        <el-image style="width: 100px; height: 100px" :src="row.skuDefaultImg" />
                    </template>
                </el-table-column>
            </el-table>
        </el-dialog>
    </div>
</template>

<script lang="ts">
export default {
    name: 'SpuList'
}
</script>

<script lang="ts" setup>
// 引入接口函数
import { ref, watch, onMounted } from 'vue'
import { Plus, Edit, Delete, InfoFilled } from '@element-plus/icons-vue'
import { reqDeleteSpuInfo, reqGetSpuListData } from '@/api/product/spu'
import { useCategoryListStore } from '@/stores/categoryList'
import { reqGetSkuListData } from '@/api/product/sku'
import { ElMessage } from 'element-plus'
const small = ref(false)
const background = ref(false)
const disabled = ref(false)
const dialogSkuTableVisible = ref(false)
const loading = ref(false)
const categoryStore = useCategoryListStore()
// 分页相关的变量
const currentPage = ref<number>(1)
const pageSize = ref<number>(3)
const total = ref<number>(1)
const spuList = ref([])
const skuList = ref([])
const dialogSkuVisible = ref(false)
const emits = defineEmits(['changeScene', 'getSpuInfo']) // 接收自定义事件

// 1. 定义一个获取spu列表数据的函数
const getSpuListData = async () => {
    const result = await reqGetSpuListData(currentPage.value, pageSize.value, categoryStore.category3Id)
    spuList.value = result.records
    total.value = result.total
    console.log(result);

}
// 2. 监听三级导航数据的变化
watch(
    () => categoryStore.category3Id,
    category3Id => {
        if (!category3Id) {
            spuList.value = [] // 赋值为空
            return // 不发送请求
        }
        getSpuListData()
    },
    { immediate: true }
)

// 分页相关的两个事件
// 3. 更改每页显示条数时触发的事件处理程序
const handleSizeChange = (val: number) => {
    // val就是每页显示的条数 其实也相当于是pageSize
    pageSize.value = val
    currentPage.value = 1
    // 重新获取页面列表数据
    getSpuListData()
}

// 4. 根据当前页码进行切换时触发的函数
const handleCurrentChange = (val: number) => {
    // console.log(`current page: ${val}`)
    currentPage.value = val
    getSpuListData()
}

// 5. 删除spu
const deleteSpuInfo = async (id: number) => {
    await reqDeleteSpuInfo(id)
    ElMessage.success('删除spu成功...')

    // 重新刷新页面
    getSpuListData()
}

// 6. 更新spu之数据回显
const toEditSpuInfo = row => {
    emits('changeScene', 2)
    emits('getSpuInfo', {
        ...row,
        spuImageList: [], // 把缺少的两项，手动补上
        spuSaleAttrList: []
    })
}

// 7. 去添加spuInfo
const toAddSpuInfo = () => {

    // 跳转到spuForm.vue页面
    emits('changeScene', 2)

    // 清空原来的旧数据
    emits('getSpuInfo', {})
}

// 8. 去添加skuInfo 也就是要跳转到SkuForm.vue
const toAddSkuInfo = (row) => {
    emits('getSpuInfo', row)
    emits('changeScene', 3)
}

// 9. 查看sku列表
const toCheckSkuList = async (id: number) => {
    const result = await reqGetSkuListData(id)
    // console.log('rr',result)
    dialogSkuVisible.value = true
    skuList.value = result
}
</script>

<style>
</style>